home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / vidhrdw / galaxian.c < prev    next >
C/C++ Source or Header  |  2000-05-04  |  26KB  |  1,155 lines

  1. /***************************************************************************
  2.  
  3.   vidhrdw.c
  4.  
  5.   Functions to emulate the video hardware of the machine.
  6.  
  7.   This is the driver for the "Galaxian" style board, used, with small
  8.   variations, by an incredible amount of games in the early 80s.
  9.  
  10.   This video driver is used by the following drivers:
  11.   - galaxian.c
  12.   - mooncrst.c
  13.   - scramble.c
  14.   - scobra.c
  15.  
  16. ***************************************************************************/
  17.  
  18. #include "driver.h"
  19. #include "vidhrdw/generic.h"
  20.  
  21.  
  22. static struct rectangle _spritevisiblearea =
  23. {
  24.     2*8+1, 32*8-1,
  25.     2*8, 30*8-1
  26. };
  27. static struct rectangle _spritevisibleareaflipx =
  28. {
  29.     0*8, 30*8-2,
  30.     2*8, 30*8-1
  31. };
  32.  
  33. static struct rectangle* spritevisiblearea;
  34. static struct rectangle* spritevisibleareaflipx;
  35.  
  36.  
  37. #define MAX_STARS 250
  38. #define STARS_COLOR_BASE 32
  39.  
  40. unsigned char *galaxian_attributesram;
  41. unsigned char *galaxian_bulletsram;
  42.  
  43. size_t galaxian_bulletsram_size;
  44. static int stars_on,stars_blink;
  45. static int stars_type;        /* -1 = no stars */
  46.                             /*  0 = Galaxian stars */
  47.                             /*  1 = Scramble stars */
  48.                             /*  2 = Rescue stars (same as Scramble, but only half screen) */
  49.                             /*  3 = Mariner stars (same as Galaxian, but some parts are blanked */
  50.                             /*  5 = Jumpbug stars */
  51. static unsigned int stars_scroll;
  52. static int color_mask;
  53.  
  54. struct star
  55. {
  56.     int x,y,code;
  57. };
  58. static struct star stars[MAX_STARS];
  59. static int total_stars;
  60. static void (*modify_charcode  )(int*,int);            /* function to call to do character banking */
  61. static void (*modify_spritecode)(int*,int*,int*,int);  /* function to call to do sprite banking */
  62. static int mooncrst_gfxextend;
  63. static int pisces_gfxbank;
  64. static int jumpbug_gfxbank[5];
  65. static int flipscreen[2];
  66.  
  67. static int background_on;
  68. static unsigned char backcolor[256];
  69.  
  70. static WRITE_HANDLER( mooncrgx_gfxextend_w );
  71.  
  72. static void mooncrst_modify_charcode  (int *charcode,int offs);
  73. static void  moonqsr_modify_charcode  (int *charcode,int offs);
  74. static void   pisces_modify_charcode  (int *charcode,int offs);
  75. static void  mariner_modify_charcode  (int *charcode,int offs);
  76. static void  jumpbug_modify_charcode  (int *charcode,int offs);
  77.  
  78. static void mooncrst_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs);
  79. static void  moonqsr_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs);
  80. static void   ckongs_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs);
  81. static void  calipso_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs);
  82. static void   pisces_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs);
  83. static void  jumpbug_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs);
  84.  
  85. /***************************************************************************
  86.  
  87.   Convert the color PROMs into a more useable format.
  88.  
  89.   Galaxian has one 32 bytes palette PROM, connected to the RGB output this way:
  90.  
  91.   bit 7 -- 220 ohm resistor  -- BLUE
  92.         -- 470 ohm resistor  -- BLUE
  93.         -- 220 ohm resistor  -- GREEN
  94.         -- 470 ohm resistor  -- GREEN
  95.         -- 1  kohm resistor  -- GREEN
  96.         -- 220 ohm resistor  -- RED
  97.         -- 470 ohm resistor  -- RED
  98.   bit 0 -- 1  kohm resistor  -- RED
  99.  
  100.   The output of the background star generator is connected this way:
  101.  
  102.   bit 5 -- 100 ohm resistor  -- BLUE
  103.         -- 150 ohm resistor  -- BLUE
  104.         -- 100 ohm resistor  -- GREEN
  105.         -- 150 ohm resistor  -- GREEN
  106.         -- 100 ohm resistor  -- RED
  107.   bit 0 -- 150 ohm resistor  -- RED
  108.  
  109.   The blue background in Scramble and other games goes through a 390 ohm
  110.   resistor.
  111.  
  112.   The RGB outputs have a 470 ohm pull-down each.
  113.  
  114. ***************************************************************************/
  115. void galaxian_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  116. {
  117.     int i;
  118.     #define TOTAL_COLORS(gfxn) (Machine->gfx[gfxn]->total_colors * Machine->gfx[gfxn]->color_granularity)
  119.     #define COLOR(gfxn,offs) (colortable[Machine->drv->gfxdecodeinfo[gfxn].color_codes_start + offs])
  120.  
  121.  
  122.     color_mask = (Machine->gfx[0]->color_granularity == 4) ? 7 : 3;
  123.  
  124.     /* first, the character/sprite palette */
  125.     for (i = 0;i < 32;i++)
  126.     {
  127.         int bit0,bit1,bit2;
  128.  
  129.         /* red component */
  130.         bit0 = (*color_prom >> 0) & 0x01;
  131.         bit1 = (*color_prom >> 1) & 0x01;
  132.         bit2 = (*color_prom >> 2) & 0x01;
  133.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  134.         /* green component */
  135.         bit0 = (*color_prom >> 3) & 0x01;
  136.         bit1 = (*color_prom >> 4) & 0x01;
  137.         bit2 = (*color_prom >> 5) & 0x01;
  138.         *(palette++) = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
  139.         /* blue component */
  140.         bit0 = (*color_prom >> 6) & 0x01;
  141.         bit1 = (*color_prom >> 7) & 0x01;
  142.         *(palette++) = 0x4f * bit0 + 0xa8 * bit1;
  143.  
  144.         color_prom++;
  145.     }
  146.  
  147.     /* now the stars */
  148.     for (i = 0;i < 64;i++)
  149.     {
  150.         int bits;
  151.         int map[4] = { 0x00, 0x88, 0xcc, 0xff };
  152.  
  153.  
  154.         bits = (i >> 0) & 0x03;
  155.         *(palette++) = map[bits];
  156.         bits = (i >> 2) & 0x03;
  157.         *(palette++) = map[bits];
  158.         bits = (i >> 4) & 0x03;
  159.         *(palette++) = map[bits];
  160.     }
  161.  
  162.     /* characters and sprites use the same palette */
  163.     for (i = 0;i < TOTAL_COLORS(0);i++)
  164.     {
  165.         /* 00 is always mapped to pen 0 */
  166.         if ((i & (Machine->gfx[0]->color_granularity - 1)) == 0)  COLOR(0,i) = 0;
  167.     }
  168.  
  169.     /* bullets can be either white or yellow */
  170.  
  171.     COLOR(2,0) = 0;
  172.     COLOR(2,1) = 0x0f + STARS_COLOR_BASE;    /* yellow */
  173.     COLOR(2,2) = 0;
  174.     COLOR(2,3) = 0x3f + STARS_COLOR_BASE;    /* white */
  175.  
  176.     /* default blue background */
  177.     *(palette++) = 0;
  178.     *(palette++) = 0;
  179.     *(palette++) = 0x55;
  180.  
  181.     for (i = 0;i < TOTAL_COLORS(3);i++)
  182.     {
  183.         COLOR(3,i) = 96 + (i % (Machine->drv->total_colors - 96));
  184.     }
  185. }
  186.  
  187. void minefld_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  188. {
  189.     int i;
  190.  
  191.  
  192.     galaxian_vh_convert_color_prom(palette, colortable, color_prom);
  193.  
  194.     /* set up background colors */
  195.  
  196.        /* Graduated Blue */
  197.  
  198.        for (i = 0; i < 64; i++)
  199.     {
  200.         palette[96*3 + i*3 + 0] = 0;
  201.            palette[96*3 + i*3 + 1] = i * 2;
  202.            palette[96*3 + i*3 + 2] = i * 4;
  203.     }
  204.  
  205.     /* Graduated Brown */
  206.  
  207.        for (i = 0; i < 64; i++)
  208.     {
  209.            palette[160*3 + i*3 + 0] = i * 3;
  210.            palette[160*3 + i*3 + 1] = i * 1.5;
  211.            palette[160*3 + i*3 + 2] = i;
  212.     }
  213. }
  214.  
  215. void rescue_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  216. {
  217.     int i;
  218.  
  219.  
  220.     galaxian_vh_convert_color_prom(palette, colortable, color_prom);
  221.  
  222.     /* set up background colors */
  223.  
  224.        /* Graduated Blue */
  225.  
  226.        for (i = 0; i < 64; i++)
  227.     {
  228.         palette[96*3 + i*3 + 0] = 0;
  229.            palette[96*3 + i*3 + 1] = i * 2;
  230.            palette[96*3 + i*3 + 2] = i * 4;
  231.     }
  232. }
  233.  
  234. void stratgyx_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  235. {
  236.     galaxian_vh_convert_color_prom(palette, colortable, color_prom);
  237.  
  238.     /* set up background colors */
  239.  
  240.        /* blue and dark brown */
  241.  
  242.     palette[96*3 + 0] = 0;
  243.     palette[96*3 + 1] = 0;
  244.     palette[96*3 + 2] = 0x55;
  245.  
  246.     palette[97*3 + 0] = 0x40;
  247.     palette[97*3 + 1] = 0x20;
  248.     palette[97*3 + 2] = 0x0;
  249. }
  250.  
  251. void mariner_vh_convert_color_prom(unsigned char *palette, unsigned short *colortable,const unsigned char *color_prom)
  252. {
  253.     int i;
  254.  
  255.     galaxian_vh_convert_color_prom(palette, colortable, color_prom);
  256.  
  257.     /* set up background colors */
  258.  
  259.        /* 16 shades of blue - the 4 bits are connected to the following resistors
  260.  
  261.           bit 0 -- 4.7 kohm resistor
  262.               -- 2.2 kohm resistor
  263.               -- 1   kohm resistor
  264.           bit 0 -- .47 kohm resistor */
  265.  
  266.        for (i = 0; i < 16; i++)
  267.     {
  268.         int bit0,bit1,bit2,bit3;
  269.  
  270.         bit0 = (i >> 0) & 0x01;
  271.         bit1 = (i >> 1) & 0x01;
  272.         bit2 = (i >> 2) & 0x01;
  273.         bit3 = (i >> 3) & 0x01;
  274.  
  275.         palette[96*3 + i*3 + 0] = 0;
  276.            palette[96*3 + i*3 + 1] = 0;
  277.            palette[96*3 + i*3 + 2] = 0x0e * bit0 + 0x1f * bit1 + 0x43 * bit2 + 0x8f * bit3;
  278.     }
  279. }
  280.  
  281. static void decode_background(void)
  282. {
  283.     int i, j, k;
  284.     unsigned char tile[32*8*8];
  285.  
  286.  
  287.     for (i = 0; i < 32; i++)
  288.     {
  289.         for (j = 0; j < 8; j++)
  290.         {
  291.             for (k = 0; k < 8; k++)
  292.             {
  293.                 tile[i*64 + j*8 + k] = backcolor[i*8+j];
  294.             }
  295.         }
  296.  
  297.         decodechar(Machine->gfx[3],i,tile,Machine->drv->gfxdecodeinfo[3].gfxlayout);
  298.     }
  299. }
  300.  
  301. /***************************************************************************
  302.  
  303.   Start the video hardware emulation.
  304.  
  305. ***************************************************************************/
  306.  
  307. static int common_vh_start(void)
  308. {
  309.     extern struct GameDriver driver_newsin7;
  310.     int generator;
  311.     int x,y;
  312.  
  313.  
  314.     modify_charcode   = 0;
  315.     modify_spritecode = 0;
  316.  
  317.     mooncrst_gfxextend = 0;
  318.     stars_on = 0;
  319.     flipscreen[0] = 0;
  320.     flipscreen[1] = 0;
  321.  
  322.     if (generic_vh_start() != 0)
  323.         return 1;
  324.  
  325.     /* Default alternate background - Solid Blue */
  326.  
  327.     for (x=0; x<256; x++)
  328.     {
  329.         backcolor[x] = 0;
  330.     }
  331.     background_on = 0;
  332.  
  333.     decode_background();
  334.  
  335.  
  336.     /* precalculate the star background */
  337.  
  338.     total_stars = 0;
  339.     generator = 0;
  340.  
  341.     for (y = 255;y >= 0;y--)
  342.     {
  343.         for (x = 511;x >= 0;x--)
  344.         {
  345.             int bit1,bit2;
  346.  
  347.  
  348.             generator <<= 1;
  349.             bit1 = (~generator >> 17) & 1;
  350.             bit2 = (generator >> 5) & 1;
  351.  
  352.             if (bit1 ^ bit2) generator |= 1;
  353.  
  354.             if (((~generator >> 16) & 1) && (generator & 0xff) == 0xff)
  355.             {
  356.                 int color;
  357.  
  358.                 color = (~(generator >> 8)) & 0x3f;
  359.                 if (color && total_stars < MAX_STARS)
  360.                 {
  361.                     stars[total_stars].x = x;
  362.                     stars[total_stars].y = y;
  363.                     stars[total_stars].code = color;
  364.  
  365.                     total_stars++;
  366.                 }
  367.             }
  368.         }
  369.     }
  370.  
  371.  
  372.     /* all the games except New Sinbad 7 clip the sprites at the top of the screen,
  373.        New Sinbad 7 does it at the bottom */
  374.     if (Machine->gamedrv == &driver_newsin7)
  375.     {
  376.         spritevisiblearea      = &_spritevisibleareaflipx;
  377.         spritevisibleareaflipx = &_spritevisiblearea;
  378.     }
  379.     else
  380.     {
  381.         spritevisiblearea      = &_spritevisiblearea;
  382.         spritevisibleareaflipx = &_spritevisibleareaflipx;
  383.     }
  384.  
  385.  
  386.     return 0;
  387. }
  388.  
  389. int galaxian_vh_start(void)
  390. {
  391.     stars_type = 0;
  392.     return common_vh_start();
  393. }
  394.  
  395. int mooncrst_vh_start(void)
  396. {
  397.     int ret = galaxian_vh_start();
  398.  
  399.     modify_charcode   = mooncrst_modify_charcode;
  400.     modify_spritecode = mooncrst_modify_spritecode;
  401.     return ret;
  402. }
  403.  
  404. int mooncrgx_vh_start(void)
  405. {
  406.     install_mem_write_handler(0, 0x6000, 0x6002, mooncrgx_gfxextend_w);
  407.     return mooncrst_vh_start();
  408. }
  409.  
  410. int moonqsr_vh_start(void)
  411. {
  412.     int ret = galaxian_vh_start();
  413.  
  414.     modify_charcode   = moonqsr_modify_charcode;
  415.     modify_spritecode = moonqsr_modify_spritecode;
  416.     return ret;
  417. }
  418.  
  419. int pisces_vh_start(void)
  420. {
  421.     int ret = galaxian_vh_start();
  422.  
  423.     modify_charcode   = pisces_modify_charcode;
  424.     modify_spritecode = pisces_modify_spritecode;
  425.     return ret;
  426. }
  427.  
  428. int scramble_vh_start(void)
  429. {
  430.     stars_type = 1;
  431.     return common_vh_start();
  432. }
  433.  
  434. int rescue_vh_start(void)
  435. {
  436.     int x;
  437.  
  438.     int ret = common_vh_start();
  439.  
  440.     stars_type = 2;
  441.  
  442.     /* Setup background color array (blue sky, blue sea, black bottom line) */
  443.  
  444.     for (x=0;x<64;x++)
  445.     {
  446.         backcolor[x*2+0] = x;
  447.         backcolor[x*2+1] = x;
  448.     }
  449.  
  450.     for (x=0;x<60;x++)
  451.     {
  452.         backcolor[128+x*2+0] = x + 4;
  453.         backcolor[128+x*2+1] = x + 4;
  454.     }
  455.  
  456.     for (x=248;x<256;x++) backcolor[x] = 0;
  457.  
  458.     decode_background();
  459.  
  460.     return ret;
  461. }
  462.  
  463. int minefld_vh_start(void)
  464. {
  465.     int x;
  466.  
  467.     int ret = common_vh_start();
  468.  
  469.     stars_type = 2;
  470.  
  471.     /* Setup background color array (blue sky, brown ground, black bottom line) */
  472.  
  473.     for (x=0;x<64;x++)
  474.     {
  475.         backcolor[x*2+0] = x;
  476.         backcolor[x*2+1] = x;
  477.     }
  478.  
  479.     for (x=0;x<60;x++)
  480.     {
  481.         backcolor[128+x*2+0] = x + 64;
  482.         backcolor[128+x*2+1] = x + 64;
  483.     }
  484.  
  485.     for (x=248;x<256;x++) backcolor[x] = 0;
  486.  
  487.     decode_background();
  488.  
  489.     return ret;
  490. }
  491.  
  492. int stratgyx_vh_start(void)
  493. {
  494.     int x;
  495.  
  496.     int ret = common_vh_start();
  497.  
  498.     stars_type = -1;
  499.  
  500.     /* Setup background color array (blue left side, brown ground */
  501.  
  502.     for (x=0;x<48;x++)
  503.     {
  504.         backcolor[x] = 0;
  505.     }
  506.  
  507.     for (x=48;x<256;x++)
  508.     {
  509.         backcolor[x] = 1;
  510.     }
  511.  
  512.     decode_background();
  513.  
  514.     return ret;
  515. }
  516.  
  517. int ckongs_vh_start(void)
  518. {
  519.     int ret = common_vh_start();
  520.  
  521.     stars_type = 1;
  522.     modify_spritecode = ckongs_modify_spritecode;
  523.     return ret;
  524. }
  525.  
  526. int calipso_vh_start(void)
  527. {
  528.     int ret = common_vh_start();
  529.  
  530.     stars_type = 1;
  531.     modify_spritecode = calipso_modify_spritecode;
  532.     return ret;
  533. }
  534.  
  535. int mariner_vh_start(void)
  536. {
  537.     int x;
  538.     unsigned char *background_prom;
  539.  
  540.  
  541.     int ret = common_vh_start();
  542.  
  543.     stars_type = 3;
  544.     modify_charcode = mariner_modify_charcode;
  545.  
  546.  
  547.     /* setup background color array (blue water).
  548.        The 2nd 32 bytes of the PROM is for the flipped screen,
  549.        it's emulated indirectly */
  550.  
  551.     background_prom = memory_region(REGION_USER1);
  552.  
  553.     for (x = 1; x < 32; x++)
  554.     {
  555.         int i;
  556.  
  557.         for (i = 0; i < 8; i++)
  558.         {
  559.             backcolor[(x-1)*8+i] = background_prom[x];
  560.         }
  561.     }
  562.  
  563.     for (x=248;x<256;x++) backcolor[x] = 0;
  564.  
  565.     decode_background();
  566.  
  567.     /* The background is always on */
  568.     background_on = 1;
  569.  
  570.     return ret;
  571. }
  572.  
  573. int jumpbug_vh_start(void)
  574. {
  575.     int ret = common_vh_start();
  576.  
  577.     stars_type = 5;
  578.  
  579.     modify_charcode   = jumpbug_modify_charcode;
  580.     modify_spritecode = jumpbug_modify_spritecode;
  581.     return ret;
  582. }
  583.  
  584. int zigzag_vh_start(void)
  585. {
  586.     int ret = galaxian_vh_start();
  587.  
  588.     /* no bullets RAM */
  589.     galaxian_bulletsram_size = 0;
  590.     return ret;
  591. }
  592.  
  593.  
  594.  
  595. WRITE_HANDLER( galaxian_flipx_w )
  596. {
  597.     if (flipscreen[0] != (data & 1))
  598.     {
  599.         flipscreen[0] = data & 1;
  600.         memset(dirtybuffer,1,videoram_size);
  601.     }
  602. }
  603.  
  604. WRITE_HANDLER( galaxian_flipy_w )
  605. {
  606.     if (flipscreen[1] != (data & 1))
  607.     {
  608.         flipscreen[1] = data & 1;
  609.         memset(dirtybuffer,1,videoram_size);
  610.     }
  611. }
  612.  
  613. WRITE_HANDLER( hotshock_flipscreen_w )
  614. {
  615.     galaxian_flipx_w(offset, data);
  616.     galaxian_flipy_w(offset, data);
  617. }
  618.  
  619.  
  620. WRITE_HANDLER( galaxian_attributes_w )
  621. {
  622.     if ((offset & 1) && galaxian_attributesram[offset] != data)
  623.     {
  624.         int i;
  625.  
  626.  
  627.         for (i = offset / 2;i < videoram_size;i += 32)
  628.             dirtybuffer[i] = 1;
  629.     }
  630.  
  631.     galaxian_attributesram[offset] = data;
  632. }
  633.  
  634.  
  635. WRITE_HANDLER( scramble_background_w )
  636. {
  637.     if (background_on != data)
  638.     {
  639.         background_on = data;
  640.         memset(dirtybuffer,1,videoram_size);
  641.     }
  642. }
  643.  
  644.  
  645. WRITE_HANDLER( galaxian_stars_w )
  646. {
  647.     stars_on = (data & 1);
  648.     stars_scroll = 0;
  649. }
  650.  
  651.  
  652. WRITE_HANDLER( mooncrst_gfxextend_w )
  653. {
  654.     if (data) mooncrst_gfxextend |= (1 << offset);
  655.     else mooncrst_gfxextend &= ~(1 << offset);
  656. }
  657.  
  658.  
  659. static WRITE_HANDLER( mooncrgx_gfxextend_w )
  660. {
  661.   /* for the Moon Cresta bootleg on Galaxian H/W the gfx_extend is
  662.      located at 0x6000-0x6002.  Also, 0x6000 and 0x6001 are reversed. */
  663.      if(offset == 1)
  664.        offset = 0;
  665.      else if(offset == 0)
  666.        offset = 1;    /* switch 0x6000 and 0x6001 */
  667.     mooncrst_gfxextend_w(offset, data);
  668. }
  669.  
  670. WRITE_HANDLER( pisces_gfxbank_w )
  671. {
  672.     if (pisces_gfxbank != (data & 1))
  673.     {
  674.         pisces_gfxbank = data & 1;
  675.         memset(dirtybuffer,1,videoram_size);
  676.     }
  677. }
  678.  
  679. WRITE_HANDLER( jumpbug_gfxbank_w )
  680. {
  681.     if (jumpbug_gfxbank[offset] != data)
  682.     {
  683.         jumpbug_gfxbank[offset] = data;
  684.         memset(dirtybuffer,1,videoram_size);
  685.     }
  686. }
  687.  
  688.  
  689. INLINE void plot_star(struct osd_bitmap *bitmap, int x, int y, int code)
  690. {
  691.     int backcol, pixel;
  692.  
  693.     backcol = backcolor[x];
  694.  
  695.     if (flipscreen[0])
  696.     {
  697.         x = 255 - x;
  698.     }
  699.     if (flipscreen[1])
  700.     {
  701.         y = 255 - y;
  702.     }
  703.  
  704.     pixel = read_pixel(bitmap, x, y);
  705.  
  706.     if ((pixel == Machine->pens[0]) ||
  707.         (pixel == Machine->pens[96 + backcol]))
  708.     {
  709.         plot_pixel(bitmap, x, y, Machine->pens[STARS_COLOR_BASE + code]);
  710.     }
  711. }
  712.  
  713.  
  714. /* Character banking routines */
  715. static void mooncrst_modify_charcode(int *charcode,int offs)
  716. {
  717.     if ((mooncrst_gfxextend & 4) && (*charcode & 0xc0) == 0x80)
  718.     {
  719.         *charcode = (*charcode & 0x3f) | (mooncrst_gfxextend << 6);
  720.     }
  721. }
  722.  
  723. static void moonqsr_modify_charcode(int *charcode,int offs)
  724. {
  725.     if (galaxian_attributesram[2 * (offs % 32) + 1] & 0x20)
  726.     {
  727.         *charcode += 256;
  728.     }
  729.  
  730.     mooncrst_modify_charcode(charcode,offs);
  731. }
  732.  
  733. static void pisces_modify_charcode(int *charcode,int offs)
  734. {
  735.     if (pisces_gfxbank)
  736.     {
  737.         *charcode += 256;
  738.     }
  739. }
  740.  
  741. static void mariner_modify_charcode(int *charcode,int offs)
  742. {
  743.     /* I don't really know if this is correct, but I don't see
  744.        any other obvious way to switch character banks. */
  745.     if (((offs & 0x1f) <= 4) ||
  746.         ((offs & 0x1f) >= 30))
  747.     {
  748.         *charcode += 256;
  749.     }
  750. }
  751.  
  752. static void jumpbug_modify_charcode(int *charcode,int offs)
  753. {
  754.     if (((*charcode & 0xc0) == 0x80) &&
  755.          (jumpbug_gfxbank[2] & 1) != 0)
  756.     {
  757.         *charcode += 128 + (( jumpbug_gfxbank[0] & 1) << 6) +
  758.                            (( jumpbug_gfxbank[1] & 1) << 7) +
  759.                            ((~jumpbug_gfxbank[4] & 1) << 8);
  760.     }
  761. }
  762.  
  763.  
  764. /* Sprite banking routines */
  765. static void mooncrst_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs)
  766. {
  767.     if ((mooncrst_gfxextend & 4) && (*spritecode & 0x30) == 0x20)
  768.     {
  769.         *spritecode = (*spritecode & 0x0f) | (mooncrst_gfxextend << 4);
  770.     }
  771. }
  772.  
  773. static void moonqsr_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs)
  774. {
  775.     if (spriteram[offs + 2] & 0x20)
  776.     {
  777.         *spritecode += 64;
  778.     }
  779.  
  780.     mooncrst_modify_spritecode(spritecode, flipx, flipy, offs);
  781. }
  782.  
  783. static void ckongs_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs)
  784. {
  785.     if (spriteram[offs + 2] & 0x10)
  786.     {
  787.         *spritecode += 64;
  788.     }
  789. }
  790.  
  791. static void calipso_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs)
  792. {
  793.     /* No flips */
  794.     *spritecode = spriteram[offs + 1];
  795.     *flipx = 0;
  796.     *flipy = 0;
  797. }
  798.  
  799. static void pisces_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs)
  800. {
  801.     if (pisces_gfxbank)
  802.     {
  803.         *spritecode += 64;
  804.     }
  805. }
  806.  
  807. static void jumpbug_modify_spritecode(int *spritecode,int *flipx,int *flipy,int offs)
  808. {
  809.     if (((*spritecode & 0x30) == 0x20) &&
  810.          (jumpbug_gfxbank[2] & 1) != 0)
  811.     {
  812.         *spritecode += 32 + (( jumpbug_gfxbank[0] & 1) << 4) +
  813.                             (( jumpbug_gfxbank[1] & 1) << 5) +
  814.                             ((~jumpbug_gfxbank[4] & 1) << 6);
  815.     }
  816. }
  817.  
  818. /***************************************************************************
  819.  
  820.   Draw the game screen in the given osd_bitmap.
  821.   Do NOT call osd_update_display() from this function, it will be called by
  822.   the main emulation engine.
  823.  
  824. ***************************************************************************/
  825. void galaxian_vh_screenrefresh(struct osd_bitmap *bitmap,int full_refresh)
  826. {
  827.     int i,offs;
  828.  
  829.     /* for every character in the Video RAM, check if it has been modified */
  830.     /* since last time and update it accordingly. */
  831.     for (offs = videoram_size - 1;offs >= 0;offs--)
  832.     {
  833.         if (dirtybuffer[offs])
  834.         {
  835.             int sx,sy,charcode,background_charcode;
  836.  
  837.  
  838.             dirtybuffer[offs] = 0;
  839.  
  840.             sx = offs % 32;
  841.             sy = offs / 32;
  842.  
  843.             background_charcode = sx;
  844.  
  845.             charcode = videoram[offs];
  846.  
  847.             if (flipscreen[0]) sx = 31 - sx;
  848.             if (flipscreen[1]) sy = 31 - sy;
  849.  
  850.             if (modify_charcode)
  851.             {
  852.                 modify_charcode(&charcode, offs);
  853.             }
  854.  
  855.             if (background_on)
  856.             {
  857.                 /* Draw background */
  858.  
  859.                  drawgfx(tmpbitmap,Machine->gfx[3],
  860.                         background_charcode,
  861.                         0,
  862.                         flipscreen[0],flipscreen[1],
  863.                         8*sx,8*sy,
  864.                         0,TRANSPARENCY_NONE,0);
  865.             }
  866.  
  867.              drawgfx(tmpbitmap,Machine->gfx[0],
  868.                     charcode,
  869.                     galaxian_attributesram[2 * (offs % 32) + 1] & color_mask,
  870.                     flipscreen[0],flipscreen[1],
  871.                     8*sx,8*sy,
  872.                     0, background_on ? TRANSPARENCY_COLOR : TRANSPARENCY_NONE, 0);
  873.         }
  874.     }
  875.  
  876.  
  877.     /* copy the temporary bitmap to the screen */
  878.     {
  879.         int scroll[32];
  880.  
  881.  
  882.         if (flipscreen[0])
  883.         {
  884.             for (i = 0;i < 32;i++)
  885.             {
  886.                 scroll[31-i] = -galaxian_attributesram[2 * i];
  887.                 if (flipscreen[1]) scroll[31-i] = -scroll[31-i];
  888.             }
  889.         }
  890.         else
  891.         {
  892.             for (i = 0;i < 32;i++)
  893.             {
  894.                 scroll[i] = -galaxian_attributesram[2 * i];
  895.                 if (flipscreen[1]) scroll[i] = -scroll[i];
  896.             }
  897.         }
  898.  
  899.         copyscrollbitmap(bitmap,tmpbitmap,0,0,32,scroll,&Machine->drv->visible_area,TRANSPARENCY_NONE,0);
  900.     }
  901.  
  902.  
  903.     /* draw the bullets */
  904.     for (offs = 0;offs < galaxian_bulletsram_size;offs += 4)
  905.     {
  906.         int x,y;
  907.         int color;
  908.  
  909.  
  910.         if (offs == 7*4) color = 0;    /* yellow */
  911.         else color = 1;    /* white */
  912.  
  913.         x = 255 - galaxian_bulletsram[offs + 3] - Machine->drv->gfxdecodeinfo[2].gfxlayout->width;
  914.         y = 255 - galaxian_bulletsram[offs + 1];
  915.         if (flipscreen[1]) y = 255 - y;
  916.  
  917.         drawgfx(bitmap,Machine->gfx[2],
  918.                 0,    /* this is just a line, generated by the hardware */
  919.                 color,
  920.                 0,0,
  921.                 x,y,
  922.                 &Machine->drv->visible_area,TRANSPARENCY_PEN,0);
  923.     }
  924.  
  925.  
  926.     /* Draw the sprites */
  927.     for (offs = spriteram_size - 4;offs >= 0;offs -= 4)
  928.     {
  929.         int flipx,flipy,sx,sy,spritecode;
  930.  
  931.  
  932.         sx = (spriteram[offs + 3] + 1) & 0xff; /* This is definately correct in Mariner. Look at
  933.                                                   the 'gate' moving up/down. It stops at the
  934.                                                     right spots */
  935.         sy = 240 - spriteram[offs];
  936.         flipx = spriteram[offs + 1] & 0x40;
  937.         flipy = spriteram[offs + 1] & 0x80;
  938.         spritecode = spriteram[offs + 1] & 0x3f;
  939.  
  940.         if (modify_spritecode)
  941.         {
  942.             modify_spritecode(&spritecode, &flipx, &flipy, offs);
  943.         }
  944.  
  945.         if (flipscreen[0])
  946.         {
  947.             sx = 240 - sx;    /* I checked a bunch of games including Scramble
  948.                                (# of pixels the ship is from the top of the mountain),
  949.                                Mariner and Checkman. This is correct for them */
  950.             flipx = !flipx;
  951.         }
  952.         if (flipscreen[1])
  953.         {
  954.             sy = 240 - sy;
  955.             flipy = !flipy;
  956.         }
  957.  
  958.         /* In Amidar, */
  959.         /* Sprites #0, #1 and #2 need to be offset one pixel to be correctly */
  960.         /* centered on the ladders in Turtles (we move them down, but since this */
  961.         /* is a rotated game, we actually move them left). */
  962.         /* Note that the adjustment must be done AFTER handling flipscreen, thus */
  963.         /* proving that this is a hardware related "feature" */
  964.         /* This is not Amidar, it is Galaxian/Scramble/hundreds of clones, and I'm */
  965.         /* not sure it should be the same. A good game to test alignment is Armored Car */
  966. /*        if (offs <= 2*4) sy++;*/
  967.  
  968.         drawgfx(bitmap,Machine->gfx[1],
  969.                 spritecode,
  970.                 spriteram[offs + 2] & color_mask,
  971.                 flipx,flipy,
  972.                 sx,sy,
  973.                 flipscreen[0] ? spritevisibleareaflipx : spritevisiblearea,TRANSPARENCY_PEN,0);
  974.     }
  975.  
  976.  
  977.     /* draw the stars */
  978.     if (stars_on)
  979.     {
  980.         switch (stars_type)
  981.         {
  982.         case -1: /* no stars */
  983.             break;
  984.  
  985.         case 0:    /* Galaxian stars */
  986.         case 3:    /* Mariner stars */
  987.             for (offs = 0;offs < total_stars;offs++)
  988.             {
  989.                 int x,y;
  990.  
  991.  
  992.                 x = ((stars[offs].x + stars_scroll) % 512) / 2;
  993.                 y = (stars[offs].y + (stars_scroll + stars[offs].x) / 512) % 256;
  994.  
  995.                 if (y >= Machine->drv->visible_area.min_y &&
  996.                     y <= Machine->drv->visible_area.max_y)
  997.                 {
  998.                     /* No stars below row (column) 64, between rows 176 and 215 or
  999.                        between 224 and 247 */
  1000.                     if ((stars_type == 3) &&
  1001.                         ((x < 64) ||
  1002.                         ((x >= 176) && (x < 216)) ||
  1003.                         ((x >= 224) && (x < 248)))) continue;
  1004.  
  1005.                     if ((y & 1) ^ ((x >> 4) & 1))
  1006.                     {
  1007.                         plot_star(bitmap, x, y, stars[offs].code);
  1008.                     }
  1009.                 }
  1010.             }
  1011.             break;
  1012.  
  1013.         case 1:    /* Scramble stars */
  1014.         case 2:    /* Rescue stars */
  1015.             for (offs = 0;offs < total_stars;offs++)
  1016.             {
  1017.                 int x,y;
  1018.  
  1019.  
  1020.                 x = stars[offs].x / 2;
  1021.                 y = stars[offs].y;
  1022.  
  1023.                 if (y >= Machine->drv->visible_area.min_y &&
  1024.                     y <= Machine->drv->visible_area.max_y)
  1025.                 {
  1026.                     if ((stars_type != 2 || x < 128) &&    /* draw only half screen in Rescue */
  1027.                        ((y & 1) ^ ((x >> 4) & 1)))
  1028.                     {
  1029.                         /* Determine when to skip plotting */
  1030.                         switch (stars_blink)
  1031.                         {
  1032.                         case 0:
  1033.                             if (!(stars[offs].code & 1))  continue;
  1034.                             break;
  1035.                         case 1:
  1036.                             if (!(stars[offs].code & 4))  continue;
  1037.                             break;
  1038.                         case 2:
  1039.                             if (!(stars[offs].x & 4))  continue;
  1040.                             break;
  1041.                         case 3:
  1042.                             /* Always plot */
  1043.                             break;
  1044.                         }
  1045.                         plot_star(bitmap, x, y, stars[offs].code);
  1046.                     }
  1047.                 }
  1048.             }
  1049.             break;
  1050.  
  1051.         case 5:    /* Jumpbug stars */
  1052.             for (offs = 0;offs < total_stars;offs++)
  1053.             {
  1054.                 int x,y;
  1055.  
  1056.  
  1057.                 x = ((stars[offs].x + stars_scroll) % 512) / 2;
  1058.                 y = (stars[offs].y + (stars_scroll + stars[offs].x) / 512) % 256;
  1059.  
  1060.                 if (y >= Machine->drv->visible_area.min_y &&
  1061.                     y <= Machine->drv->visible_area.max_y)
  1062.                 {
  1063.                     /* no stars in the status area */
  1064.                     if (x >= 240)  continue;
  1065.  
  1066.                     /* Determine when to skip plotting */
  1067.                     if ((y & 1) ^ ((x >> 4) & 1))
  1068.                     {
  1069.                         switch (stars_blink)
  1070.                         {
  1071.                         case 0:
  1072.                             if (!(stars[offs].code & 1))  continue;
  1073.                             break;
  1074.                         case 1:
  1075.                             if (!(stars[offs].code & 4))  continue;
  1076.                             break;
  1077.                         case 2:
  1078.                             if (!(stars[offs].x & 4))  continue;
  1079.                             break;
  1080.                         case 3:
  1081.                             /* Always plot */
  1082.                             break;
  1083.                         }
  1084.                         plot_star(bitmap, x, y, stars[offs].code);
  1085.                     }
  1086.                 }
  1087.             }
  1088.             break;
  1089.         }
  1090.     }
  1091. }
  1092.  
  1093.  
  1094.  
  1095. int galaxian_vh_interrupt(void)
  1096. {
  1097.     stars_scroll++;
  1098.  
  1099.     return nmi_interrupt();
  1100. }
  1101.  
  1102. int scramble_vh_interrupt(void)
  1103. {
  1104.     static int blink_count;
  1105.  
  1106.  
  1107.     blink_count++;
  1108.     if (blink_count >= 45)
  1109.     {
  1110.         blink_count = 0;
  1111.         stars_blink = (stars_blink + 1) & 3;
  1112.     }
  1113.  
  1114.     return nmi_interrupt();
  1115. }
  1116.  
  1117. int mariner_vh_interrupt(void)
  1118. {
  1119.     stars_scroll--;
  1120.  
  1121.     return nmi_interrupt();
  1122. }
  1123.  
  1124. int devilfsg_vh_interrupt(void)
  1125. {
  1126.     stars_scroll++;
  1127.  
  1128.     return interrupt();
  1129. }
  1130.  
  1131. int hunchbks_vh_interrupt(void)
  1132. {
  1133.     cpu_irq_line_vector_w(0,0,0x03);
  1134.     cpu_set_irq_line(0,0,PULSE_LINE);
  1135.  
  1136.     return ignore_interrupt();
  1137. }
  1138.  
  1139. int jumpbug_vh_interrupt(void)
  1140. {
  1141.     static int blink_count;
  1142.  
  1143.  
  1144.     stars_scroll++;
  1145.  
  1146.     blink_count++;
  1147.     if (blink_count >= 45)
  1148.     {
  1149.         blink_count = 0;
  1150.         stars_blink = (stars_blink + 1) & 3;
  1151.     }
  1152.  
  1153.     return nmi_interrupt();
  1154. }
  1155.